/*
 Copyright 2009 Abdulla Abdurakhmanov (abdulla@latestbit.com)
 Original sources are available at www.latestbit.com

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.

 StdPacketFormer.hpp. Created on: 09.09.2009 12:51:26
 */

#ifndef STDPACKETFORMER_HPP_
#define STDPACKETFORMER_HPP_

#include "smartobjects/SmartObjectsDefs.hpp"
#include "smartobjects/network/IPacketFormer.hpp"

namespace smartobjects {

class StdPacketFormer : public IPacketFormer {
public:
	SO_DLLPRFX static const unsigned int MAX_PACKET_SIZE;

public:
	SO_DLLPRFX StdPacketFormer();
	SO_DLLPRFX virtual ~StdPacketFormer();

	SO_DLLPRFX virtual void onReceiveRawPacket(ByteBuffer* buffer);
	SO_DLLPRFX virtual ByteBuffer* getReadyPacket();

protected:
	SO_DLLPRFX static const unsigned int INITIAL_CAPACITY;
	SO_DLLPRFX static const unsigned int MAX_CAPACITY;
	SO_DLLPRFX static const unsigned int INCREMENT_CAPACITY_STEP;

protected:
	ByteBuffer* m_poPacketBuffers[2];

	unsigned int m_nCurrentIdx;
	boost::uint32_t m_nCurrentLen;

	bool m_bHeaderIsReaded;
	bool m_bNeedToClear;

	unsigned int m_nCurrentBufferLen;
	unsigned int m_nCurrentReadPosition;
};

}

#endif /* STDPACKETFORMER_HPP_ */
